import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '@/store'

Vue.use(VueRouter)

const routes = [
{
  path: '/',
  component: () => import('@/views/home'),
  name: 'home',
  redirect: '/trainee',
  meta: { hidden: true, title: '首页', icon: 'el-icon-home', requiredLevel: 0 }
},
{
  path: '/login',
  component: () => import('@/views/login'),
  name: 'login',
  meta: { hidden: true, title: '登录', icon: 'el-icon-key', requiredLevel: -1 }
},
{
  path: '/trainee',
  component: () => import('@/views/trainee'),
  name: 'trainee',
  meta: { hidden: false, title: '学员', icon: 'el-icon-service', requiredLevel: 0 }
},
{
  path: '/coach',
  component: () => import('@/views/coach'),
  name: 'coach',
  meta: { hidden: false, title: '教员', icon: 'el-icon-edit', requiredLevel: 1 }
},
{
  path: '/manager',
  component: () => import('@/views/manager'),
  name: 'manager',
  meta: { hidden: false, title: '管理', icon: 'el-icon-mouse', requiredLevel: 2 }
},
{
  path: '/config',
  component: () => import('@/views/config'),
  name: 'config',
  meta: { hidden: false, title: '设置', icon: 'el-icon-setting', requiredLevel: 2 }
},
{
  path: '/reject',
  component: () => import('@/views/reject'),
  name: 'reject',
  meta: { hidden: true, title: '无权访问', icon: 'el-icon-close', requiredLevel: -1 }
}, ]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  document.title = "CDAPP-TREX " + to.meta.title;
  if ((!store || !store.state.loggedIn) && to.name != 'login') {
    next({ path: '/login' })
  } else {
    if (to.meta.requiredLevel > store.state.loginLevel && to.name != 'config') {
      next({ path: '/reject' });
    } else {
      next();
    }
  }
})

export default router